<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="../organization/function.jsp"%>
<%@ include file="function.jsp"%>
<%@ include file="../address/function.jsp"%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发送通知</title>
    <link rel="stylesheet" href="/css/public.css?cache=<%=Math.random()%>">
    <link  href="../../hos_manage/images/favicon.ico?cache=<%=Math.random()%>" rel="shortcut icon" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="../../css/palace/message/send_message.css?a=<%=Math.random()%>">
    <script src="/js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="../../UEditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="../../UEditor/ueditor.all.min.js"> </script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="../../UEditor/lang/zh-cn/zh-cn.js"></script>
    <script src="/js/components/upload.js"></script>
    <script src="/js/components/encode.js"></script>

</head>
<body id="app" v-cloak>
<%@ include file="../../header.jsp"%>
<!--内容-->
<div class="containerone">
    <%@ include file="../../left_menu.jsp"%>
    <!--右边内容-->
    <div class="right_cont">
        <!--右边导航下面内容-->
        <div class="send-message">
            <div class="title">
                <div class="address-left show-block note">
                    <span @click="homePage()" class="hover">首页</span>
                    <span class="left-distance">></span>
                    <span class="left-distance hover" @click="back()">通知列表</span>
                    <span class="left-distance">></span>
                    <span class="left-distance">添加通知</span>
                </div>
            </div>
            <div class="container">
                <div class="position">
                    <div class="show-block address-book-right">
                        <div class="data-form show-margin-left">
                            <div class="receive-object">
                                <div class="receive-object-left show-block">
                                    <div class="mem show-block" v-show="showDefault">选择接收对象</div>
                                    <div class="show-block member" v-for="i in personList" v-cloak>
                                        <img class="show-block middle" src="/images/components/add_member/ic-people.png">
                                        <div class="show-block choose-person middle" v-html="i.name"></div>
                                    </div>
                                    <div class="show-block member" v-for="i in tagList" v-cloak>
                                        <img class="show-block middle" src="/images/components/add_member/ic-lable3.png">
                                        <div class="show-block choose-person middle" v-html="i.name"></div>
                                    </div>
                                    <div class="show-block member" v-for="i in deptList" v-cloak>
                                        <img class="show-block middle" src="/images/components/add_member/ic-file2.png">
                                        <div class="show-block choose-person middle" v-html="i.deptName"></div>
                                    </div>
                                </div>
                                <div class="receive-object-right show-block" @click="addSendOrigin()">
                                    <img class="show-block" src="../../images/palace/message/ic-add.png">
                                </div>
                            </div>
                            <div class="kind">
                                <div class="notify show-block" @click="doNotify()" style="cursor: pointer;" v-bind:class="{white: notify}">
                                    <img class="show-block kind-img" src="../../images/palace/message/ic-notice2-s.png" v-show="notify">
                                    <img class="show-block kind-img" src="../../images/palace/message/ic-notice2.png" v-show="showNote">
                                    <div class="show-block">通知</div>
                                </div>
                                <div class="annouce show-block" @click="doAnnounce()" style="cursor: pointer;" v-bind:class="{white: showNote}">
                                    <img class="show-block kind-img" src="../../images/palace/message/ic-announcement.png" v-show="notify">
                                    <img class="show-block kind-img" src="../../images/palace/message/ic-announcement-s.png" v-show="showNote">
                                    <div class="show-block">公告</div>
                                </div>
                            </div>
                            <div class="notify-content" v-show="notify">
                                <div style="position: relative">
                                    <div class="common-level">等级</div>
                                    <sendsingleselected :memberlist="levelList" :choosed="selectLevel" key="name" value="selectLevel"></sendsingleselected>
                                </div>
                                <div class="common-level">允许回复</div>
                                <div>
                                    <img class="show-block width-16" src="../../images/palace/message/Radio-s.png" @click="choose()" v-show="index == 1">
                                    <img class="show-block width-16" src="../../images/palace/message/Radio.png" @click="choose()" v-show="index == 0">
                                    <div class="show-block yes">是</div>
                                    <img class="show-block width-16 no" src="../../images/palace/message/Radio.png" @click="choose()" v-show="index == 1">
                                    <img class="show-block width-16 no" src="../../images/palace/message/Radio-s.png" @click="choose()" v-show="index == 0">
                                    <div class="show-block yes">否</div>
                                </div>
                                <div class="common-level">标题(必填)</div>
                                <div>
                                    <textarea class="textarea" placeholder="请输入标题(100字以内)" v-model="title"></textarea>
                                </div>
                                <div class="common-level">通知正文(必填)</div>
                                <div>
                                    <script id="editor" type="text/plain" name="content"></script>
                                </div>
                                <div class="common-level">添加附件</div>
                                <div class="show-relative show-block">
                                    <input class="add-file" type="file" @change="changeFile($event)">
                                    <div class="add show-block">添加附件</div>
                                </div>
                                <div class="show-block path" v-html="filename"></div>
                                <div class="common-level">图片(建议图片大小不超过2M)</div>
                                <div class="show-relative choose-img">
                                    <img v-for="i in img" :src="i.src">
                                    <input type="file" class="add-img" @change="changeImg($event)">
                                </div>
                                <div class="common-level">发送人</div>
                                <div>
                                    <input type="text" class="send-people" v-model="sendPerson">
                                </div>
                                <div style="text-align: center">
                                    <div class="info-save show-block" @click="save()">保存</div>
                                </div>
                            </div>
                            <div class="notify-content" v-show="showNote">
                                <div style="position: relative">
                                    <div class="common-level">等级</div>
                                    <sendsingleselected :memberlist="levelAnnounceList" :choosed="selectAnnounceLevel" key="name" value="selectAnnounceLevel"></sendsingleselected>
                                </div>
                                <div class="common-level">允许回复</div>
                                <div>
                                    <img class="show-block width-16" src="../../images/palace/message/Radio-s.png" @click="chooseAnnounce()" v-show="index1 == 1">
                                    <img class="show-block width-16" src="../../images/palace/message/Radio.png" @click="chooseAnnounce()" v-show="index1 == 0">
                                    <div class="show-block yes">是</div>
                                    <img class="show-block width-16 no" src="../../images/palace/message/Radio.png" @click="chooseAnnounce()" v-show="index1 == 1">
                                    <img class="show-block width-16 no" src="../../images/palace/message/Radio-s.png" @click="chooseAnnounce()" v-show="index1 == 0">
                                    <div class="show-block yes">否</div>
                                </div>
                                <div class="common-level">标题(必填)</div>
                                <div>
                                    <textarea class="textarea" placeholder="请输入标题(100字以内)" v-model="announceTitle"></textarea>
                                </div>
                                <div class="common-level">通知正文(必填)</div>
                                <div>
                                    <script id="editor1" type="text/plain" name="content"></script>
                                </div>
                                <div class="common-level">添加附件</div>
                                <div class="show-relative show-block">
                                    <input class="add-file" type="file" @change="changeAnnounceFile($event)">
                                    <div class="add show-block">添加附件</div>
                                </div>
                                <div class="show-block path" v-html="announceFilename"></div>
                                <div class="common-level">图片(建议图片大小不超过2M)</div>
                                <div class="show-relative choose-img">
                                    <img v-for="i in announceImg" :src="i.src">
                                    <input type="file" class="add-img" @change="changeAnnounceImg($event)">
                                </div>
                                <div class="common-level">发送人</div>
                                <div>
                                    <input type="text" class="send-people" v-model="sendAnnouncePerson">
                                </div>
                                <div style="text-align: center">
                                    <div class="info-save show-block" @click="saveAnnounce()">保存</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<delete v-if="deleteShow" :top="delete.top" :title="delete.title" :content="delete.content" :arg="delete.arg"></delete>
<dangertip v-if="dangerTipShow" :title="dangerTitle"></dangertip>
<meesagetip v-if="messageTipShow" :title="messageTitle"></meesagetip>
<sendorigin v-show="sendOriginShow"></sendorigin>
</body>
<%@ include file="/components/message.jsp"%>
<%@ include file="/components/message/send_origin.jsp"%>
<%@ include file="/components/message/message_member.jsp"%>
<%@ include file="/components/delete.jsp"%>
<%@ include file="/components/danger.jsp"%>
<script>
    var ue = UE.getEditor('editor');
    var ue1 = UE.getEditor('editor1');
    new Vue({
        el: "#app",
        data: {
            sendOriginShow: false,
            dangerTipShow: false,
            messageTipShow: false,
            personList: "",
            deptList: "",
            tagList: "",
            notify: true,
            levelList: [],
            levelAnnounceList: [],
            selectLevel: "",
            selectAnnounceLevel: "",
            announceList: [],
            noticeID: "",
            index: 1,
            index1: 1,
            filename: "",
            announceFilename: "",
            input: "",
            announceInput: "",
            img: [],
            announceImg: [],
            imgUrl: [],
            files: [],
            announceFiles: [],
            imgInput: "",
            announceImgInput: "",
            user: [],
            dept: [],
            label: [],
            allMember: {},
            title: "",
            announceTitle: "",
            sendPerson: "",
            sendAnnouncePerson: "",
            fjObj: {},
            tpObj: {},
            announceFjObj: {},
            announceTpObj: {},
            delete: {
                top: "",
                title: "",
                content: "",
                arg: {},
            },
            deleteShow: false,
        },
        methods: {
            addSendOrigin: function() {
                this.sendOriginShow = true;
            },
            doNotify: function() {
                this.notify = true;
                for (var i = 0; i < this.announceList.length; i++) {
                    if (this.announceList[i].name == "通知") {
                        this.noticeID = this.announceList[i].id;
                    }
                }
            },
            doAnnounce: function() {
                this.notify = false;
                for (var i = 0; i < this.announceList.length; i++) {
                    if (this.announceList[i].name == "公告") {
                        this.noticeID = this.announceList[i].id;
                    }
                }
            },
            homePage: function () {
                location.href = "../../index.jsp";
            },
            changeFile: function(event) {
                this.input = undefined;
                this.filename = "";
                var input = event.target;
                var file = input.files[0];
                var filename = file.name;
                if (file.size > 20000000) {
                    this.danger("文件过大，请重新选择");
                    return;
                }
                if (!(/\.doc$/.test(filename) || /\.ppt$/.test(filename) || /\.pptx$/.test(filename) || /\.docx$/.test(filename) || /\.pdf$/.test(filename) || /\.xls$/.test(filename) || /\.xlsx$/.test(filename))) {
                    this.danger("文件类型有误，重新上传");
                    return;
                }
                this.filename = filename;
                this.input = input;
            },
            changeAnnounceFile: function (event) {
                this.announceInput = undefined;
                this.announceFilename = "";
                var input = event.target;
                var file = input.files[0];
                var filename = file.name;
                if (file.size > 20000000) {
                    this.danger("文件过大，请重新选择");
                    return;
                }
                if (!(/\.doc$/.test(filename) || /\.ppt$/.test(filename) || /\.pptx$/.test(filename) || /\.docx$/.test(filename) || /\.pdf$/.test(filename) || /\.xls$/.test(filename) || /\.xlsx$/.test(filename))) {
                    this.danger("文件类型有误，重新上传");
                    return;
                }
                this.announceFilename = filename;
                this.announceInput = input;
            },
            changeImg: function (event) {
                var files = event.target.files;
                var file = files[0];
                if (!file) {
                    this.imgInput = "";
                    this.files = [];
                    this.img = [];
                    return;
                }
                var filename = file.name;
                var input = event.target;
                if (file.size > 2000000) {
                    this.danger("图片过大，请重新选择");
                    return;
                }
                if (!/\.jpg$/.test(filename) && !/\.jpeg$/.test(filename) && !/\.png$/.test(filename)) {
                    this.danger("文件类型有误，重新上传");
                    return;
                }
                this.imgInput = input;
                this.files = files;
                var img = new Image();
                window.URL = window.URL || window.webkitURL;
                if(window.URL){
                    if (!!this.files[0]) {
                        img.src = window.URL.createObjectURL(this.files[0]);
                        img.width = 200;
                        img.onload = function(e) {
                            window.URL.revokeObjectURL(this.src);
                        }
                        this.img.$set(0, {
                            file: this.files[0],
                            src: img.src
                        })
                    }
                }
            },
            changeAnnounceImg: function (event) {
                var announceFiles = event.target.files;
                var input = event.target;
                var file = announceFiles[0];
                if (!file) {
                    this.imgInput = "";
                    this.files = [];
                    this.img = [];
                    return;
                }
                var filename = file.name;
                if (file.size > 2000000) {
                    this.danger("图片过大，请重新选择");
                    return;
                }
                if (!/\.jpg$/.test(filename) && !/\.jpeg$/.test(filename) && !/\.png$/.test(filename)) {
                    this.danger("文件类型有误，重新上传");
                    return;
                }
                this.announceImgInput = input;
                this.announceFiles = announceFiles;
                var img = new Image();
                window.URL = window.URL || window.webkitURL;
                if(window.URL){
                    if (!!this.announceFiles[0]) {
                        img.src = window.URL.createObjectURL(this.announceFiles[0]);
                        img.width = 200;
                        img.onload = function(e) {
                            window.URL.revokeObjectURL(this.src);
                        }
                        this.announceImg.$set(0, {
                            file: this.announceFiles[0],
                            src: img.src
                        })
                    }
                }
            },
            back: function () {
                locationHerf("message_center");
            },
            choose: function () {
                var _this = this;
                if (_this.index == 0) {
                    _this.index = 1;
                } else {
                    _this.index = 0;
                }
            },
            // 错误提示
            danger: function(title) {
                this.dangerTitle = title;
                this.dangerTipShow = true;
            },
            chooseAnnounce: function () {
                var _this = this;
                if (_this.index1 == 0) {
                    _this.index1 = 1;
                } else {
                    _this.index1 = 0;
                }
            },
            save: function () {
                var self = this;
                if (self.dept.length == 0 && self.label.length == 0 && self.user.length == 0) {
                    self.danger("请选择接收对象!");
                    return;
                }
                if (self.selectLevel.length == 0) {
                    self.danger("请选择等级!");
                    return;
                }
                if (self.title.trim().length == 0) {
                    self.danger("标题长度不能为空!");
                    return;
                }
                if (self.title.trim().length >= 100) {
                    self.danger("标题长度不大于100!");
                    return;
                }
                if (UE.getEditor('editor').getContent().length == 0) {
                    self.danger("请填写正文!");
                    return;
                }
                if (UE.getEditor('editor').getContent().length > 100000) {
                    self.danger("正文长度不能超过100000");
                }
                if (self.sendPerson.trim().length == 0) {
                    self.danger("请填写发送人!");
                    return;
                }
                self.deleteShow = true;
                self.delete.top = "发送通知";
                self.delete.title = "确定发送通知";
                self.delete.content = "即将发送通知";
                self.delete.arg = function () {
                    fileUpLoadMulti(self.input && [self.input] || [], function (res) {
                        if (res && res.errcode == 0) {
                            var fj = res.result[0];
                            self.fjObj = [
                                {
                                    name: self.filename,
                                    url: fj,
                                }
                            ];
                        }
                        fileUpLoadMulti(self.imgInput && [self.imgInput] || [], function (res) {
                            if (res && res.errcode == "0") {
                                var tp = res.result[0];
                                self.tpObj = [
                                    {
                                        name: self.files[0].name,
                                        url: tp,
                                    }
                                ];
                            }
                            $.ajax({
                                type: "post",
                                url: location.href,
                                data: {
                                    act: "notice_create",
                                    hospital: "${user.hospital}",
                                    title: self.title,
                                    content: new Base64().encode("<html>" + "<head>" + "<meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1'>" + "</head>" + "<body>" + UE.getEditor('editor').getContent() + "</body>" + "</html>"),
                                    category: self.noticeID,
                                    level: self.selectLevel.id,
                                    is_comment: self.index,
                                    author: self.sendPerson,
                                    receiver: new Base64().encode(JSON.stringify(self.allMember)),
                                    img: new Base64().encode(JSON.stringify(self.tpObj)),
                                    attachment: new Base64().encode(JSON.stringify(self.fjObj)),
                                    operator_id: "${user.id}",
                                    operator_name: "${user.name}",
                                    from: "web",
                                },
                                success: function (res) {
                                    res = JSON.parse(res);
                                    if (res.errcode == "0") {
                                        locationHerf("message_center");
                                    } else {
                                        self.danger(res.errmsg);
                                    }
                                }
                            });
                        });
                    });
                }
            },
            saveAnnounce: function () {
                var self = this;
                if (self.dept.length == 0 && self.label.length == 0 && self.user.length == 0) {
                    self.danger("请选择接收对象!");
                    return;
                }
                if (self.selectAnnounceLevel.length == 0) {
                    self.danger("请选择等级!");
                    return;
                }
                if (self.announceTitle.trim().length == 0) {
                    self.danger("标题长度不能为空!");
                    return;
                }
                if (self.announceTitle.trim().length >= 100) {
                    self.danger("标题长度不大于100!");
                    return;
                }
                if (UE.getEditor('editor1').getContent().length == 0) {
                    self.danger("请填写正文!");
                    return;
                }
                if (UE.getEditor('editor1').getContent().length > 100000) {
                    self.danger("正文长度不能超过100000");
                }
                if (self.sendAnnouncePerson.trim().length == 0) {
                    self.danger("请填写发送人!");
                    return;
                }
                self.deleteShow = true;
                self.delete.top = "发送公告";
                self.delete.title = "确定发送公告";
                self.delete.content = "即将发送公告";
                self.delete.arg = function () {
                    fileUpLoadMulti(self.announceInput && [self.announceInput] || [], function (res) {
                        if (res && res.errcode == 0) {
                            var fj = res.result[0];
                            self.announceFjObj = [
                                {
                                    name: self.announceFilename,
                                    url: fj,
                                }
                            ];
                        }
                        fileUpLoadMulti(self.announceImgInput && [self.announceImgInput] || [], function (res) {
                            if (res && res.errcode == "0") {
                                var tp = res.result[0];
                                self.announceTpObj = [
                                    {
                                        name: self.announceFiles[0].name,
                                        url: tp,
                                    }
                                ];
                            }
                            $.ajax({
                                type: "post",
                                url: location.href,
                                data: {
                                    act: "notice_create",
                                    hospital: "${user.hospital}",
                                    title: self.announceTitle,
                                    content: new Base64().encode("<html>" + "<head>" + "<meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1'>" + "</head>" + "<body>" + UE.getEditor('editor1').getContent() + "</body>" + "</html>"),
                                    category: self.noticeID,
                                    level: self.selectAnnounceLevel.id,
                                    is_comment: self.index1,
                                    author: self.sendAnnouncePerson,
                                    receiver: new Base64().encode(JSON.stringify(self.allMember)),
                                    img: new Base64().encode(JSON.stringify(self.announceTpObj)),
                                    attachment: new Base64().encode(JSON.stringify(self.announceFjObj)),
                                    operator_id: "${user.id}",
                                    operator_name: "${user.name}",
                                    from: "web",
                                },
                                success: function (res) {
                                    res = JSON.parse(res);
                                    if (res.errcode == "0") {
                                        locationHerf("message_center");
                                    } else {
                                        self.danger(res.errmsg);
                                    }
                                }
                            });
                        });
                    });
                }
            }
        },
        events: {
            // 关闭添加成员
            closeSendOrigin: function() {
                this.sendOriginShow = false;
            },
            dangerTipOver: function() {
                this.dangerTipShow = false;
            },
            messageTipOver: function() {
                this.messageTipShow = false;
            },
            sendOrigin: function (obj) {
                this.personList = obj.person;
                this.deptList = obj.dept;
                this.tagList = obj.tag;
                this.sendOriginShow = false;
                this.user = obj.person.map(function (item) {
                    return {
                        id: item.id,
                        name: item.name,
                    }
                });
                this.dept = obj.dept.map(function (item) {
                    return {
                        id: item.id,
                        name: item.deptName,
                    }
                });
                this.label = obj.tag.map(function (item) {
                    return {
                        id: item.id,
                        name: item.name,
                    }
                });
                this.allMember.user = this.user;
                this.allMember.dept = this.dept;
                this.allMember.label = this.label;
            },
            chooseMemberResult: function (data) {
                this[data.kind] = data.selected;
            },
            deleteCancel: function() {
                this.deleteShow = false;
            },
            dangerTipOver: function() {
                this.dangerTipShow = false;
            },
        },
        computed: {
            showDefault: function () {
                if (this.personList.length == 0 && this.deptList.length == 0 && this.tagList.length == 0) {
                    return true;
                } else {
                    return false;
                }
            },
            showNote: function() {
                return !this.notify;
            }

        },
        ready: function() {
            this.dangerTipShow = false;
            var self = this;
            $.ajax({
                type: "post",
                url: "send_message.jsp",
                data: {
                    act: "categoryList",
                    hospital: "${user.hospital}",
                },
                success: function (res) {
                    res = JSON.parse(res);
                    self.announceList = res.data;
                    for (var i = 0; i < self.announceList.length; i++) {
                        if (self.announceList[i].name == "通知") {
                            self.noticeID = self.announceList[i].id;
                        }
                    }
                }
            });
            $.ajax({
                type: "post",
                url: "send_message.jsp",
                data: {
                    act: "levelList",
                    hospital: "${user.hospital}",
                },
                success: function (res) {
                    res = JSON.parse(res);
                    self.levelList = res.data;
                    self.levelAnnounceList = res.data;
                }
            });
        },
        components: {
            dangertip: dangerTip,
            meesagetip: messageTip,
            sendorigin: sendOrigin,
            sendsingleselected: sendSingleSelected,
            delete: deleteTip,
        }
    });
</script>
</html>
